package org.grottarossa.ui.core.casemanagement.history;



import java.util.ResourceBundle;

import org.grottarossa.template.data.pojo.history.HistoryList;
import org.grottarossa.template.drivers.DriverException;
import org.grottarossa.template.drivers.components.tokens.CaseManagementId;
import org.grottarossa.ui.core.ApplicationSession;
import org.grottarossa.ui.core.DummyPanel;
import org.grottarossa.ui.core.base.BasePanel;
import org.grottarossa.ui.core.components.tables.TMSFilteredTable;
import org.grottarossa.ui.core.components.tables.TMSTable;
import org.grottarossa.ui.core.container.HistoryContainer;
import org.grottarossa.ui.core.i18n.DM;

import com.vaadin.event.ItemClickEvent;
import com.vaadin.event.ItemClickEvent.ItemClickListener;
import com.vaadin.terminal.Sizeable;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

public class HistoryListPanel extends BasePanel implements ItemClickListener
{
	private static final long serialVersionUID = 602876732986547165L;

	private TMSFilteredTable m_table = null;
	HistoryList historyList = null;

	private final CaseManagementId m_id;

	public HistoryListPanel(ApplicationSession appSession, CaseManagementId id)
	{
		super(appSession, DM.instance().getBundle(HistoryListPanel.class.getName()));
		this.m_id = id;

		initializeComponents();
	}

	@Override
	protected void initializeComponents()
	{
		VerticalLayout v = new VerticalLayout();
		v.setHeight(100, Sizeable.UNITS_PERCENTAGE);
		v.setWidth(100, Sizeable.UNITS_PERCENTAGE);
		setLayout(v);

		try
		{
			historyList = m_appSession.getBuilder().getCaseManagement().getHistory(m_id);
		}
		catch (DriverException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		if (historyList.size() < 1)
			v.addComponent ( new DummyPanel(m_appSession, "Query empty!"));
		else
		{
			HistoryContainer c = new HistoryContainer(historyList);
			m_table = new TMSFilteredTable(m_appSession, c.getDataTable());
			m_table.setFieldFilterTypeAsCombo(c.MODIFIEDBY);
			m_table.addListener(this);
			m_table.setPageLength(historyList.size());
			m_table.setWidth(100, Sizeable.UNITS_PERCENTAGE);
			m_table.setHeight(100, Sizeable.UNITS_PERCENTAGE);
			v.addComponent(m_table);
		}
	}

	public void itemClick(ItemClickEvent event)
	{
		// TODO Auto-generated method stub
		Window window = new Window("Forum Edit/View");
		window.setModal(true);
		Label message = new Label("bla bla");
		window.addComponent(message);
	    getWindow().addWindow(window);
	}

}
